#!/usr/bin/env groovy
// @file     Command.groovy
// @author   Mac Radigan
// @purpose  entry point for System

package sys.tools

import java.util.ServiceLoader
import sys.tools.Tool

public class Command {

  public static void main(String[] args) {
    def cli = new CliBuilder(usage: 'system [-h]')  
    cli.h(longOpt:'help', argName:'usage information', required:false, args:0, 'show usage information')  
    OptionAccessor opt = cli.parse(args); if(!opt) { return }  

    def providers = ServiceLoader.load(Tool.class)

    if(args.size()) {
      try {
        providers.each() { tool ->
          if(tool.getName()==args[0]) {
            def newargs = new String[args.size()-1]
	    for(int argIndex=0; argIndex<args.size()-1; argIndex++) {
	      newargs[argIndex] = args[argIndex+1]
	    }
            tool.run(newargs)
          }
        }
      } catch(e) {
        e.printStackTrace()
      }
    } else {
      try {
        println "  [tools]"
        def index = 0
        providers.each() { tool ->
          println "    ${++index}) ${tool.getName()} - ${tool.getDescription()}"
        }
      } catch(e) {
        e.printStackTrace()
      }
    }
  }

}

/* *EOF* */
